package cn.xumaoyang.pet.controller;

import cn.xumaoyang.basic.exception.BusinessException;
import cn.xumaoyang.basic.util.AjaxResult;
import cn.xumaoyang.basic.util.LoginContext;
import cn.xumaoyang.basic.util.PageList;
import cn.xumaoyang.pet.domain.Pet;
import cn.xumaoyang.pet.domain.SearchMasterMsg;
import cn.xumaoyang.pet.query.SearchMasterMsgQuery;
import cn.xumaoyang.pet.service.ISearchMasterMsgService;
import cn.xumaoyang.user.domain.Logininfo;
import com.sun.net.httpserver.HttpsServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 宠物领养
 */

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {


    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


   // HttpServletRequest 获取当前登录用户的个人信息。


    /**
     * 发布宠物信息的页面点击发布，publish
     * 前端模型层传递而来的数据使用相同数据的实体类SearchMasterMsg接受
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getCurrentLoginInfo(request);
            seachMasterMsgService.publish(searchMasterMsg,logininfo.getId());
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



    //未处理宠物信息的分页
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> Pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            return seachMasterMsgService.pending(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //已经处理宠物信息的分页。
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            return seachMasterMsgService.processed(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //查询个人消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.queryUserMsg(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //处理寻主消息。
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet,LoginContext.getCurrentLoginInfo(request));
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }





}
